﻿#include "ui_mainframe.h"
#include "image_push_button.h"
#include "bigtoolbutton.h"
#include "round_widget.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedWidget>
#include <QLabel>
#include <QApplication>
#include <QToolButton>
#include <QButtonGroup>
#include <QDebug>

Ui_MainFrame::Ui_MainFrame(QObject *parent)
    : QObject(parent)
{

}

/**
 * @brief
 *  用new，对于大一点的程序来说，会启动较慢，但是很方便.
 *  可以使用懒惰创建来加速.
 * @param parent
 */

#define TOOL_BUTTON_WIDTH 90
#define TOPBAR_HEIGHT 88

void Ui_MainFrame::setupUi(ShadowWidget *widget)
{
    Q_ASSERT(NULL == widget);

    widget->setShadowWidth(0);

    m_layout_root = widget->getRootLayout();

    RoundWidget *innerFrame = new RoundWidget;
    innerFrame->setRound(0, 0);
    m_layout_root->addWidget(innerFrame);

    QVBoxLayout *layout_main = new QVBoxLayout;
    layout_main->setContentsMargins(0, 0, 0, 0); // 清除默认的内边距
    layout_main->setSpacing(0);
    innerFrame->setLayout(layout_main);

    // topbar
    QWidget *topbar_widget = new QWidget; // 用于设置topbar背景色
    topbar_widget->setMaximumHeight(TOPBAR_HEIGHT);
    layout_main->addWidget(topbar_widget);
    topbar_widget->setObjectName(QStringLiteral("topbar"));

    QHBoxLayout *layout_topbar_widget = new QHBoxLayout;
    layout_topbar_widget->setContentsMargins(0, 0, 0, 0);

    QWidget *bar_left = new QWidget;
    QWidget *bar_middle = new QWidget;
    QWidget *bar_right = new QWidget;
    layout_topbar_widget->addWidget(bar_left);
    layout_topbar_widget->addWidget(bar_middle);
    layout_topbar_widget->addWidget(bar_right);
    layout_topbar_widget->setStretch(0, 5);
    layout_topbar_widget->setStretch(1, 10);
    layout_topbar_widget->setStretch(2, 5);

    topbar_widget->setLayout(layout_topbar_widget);

    // 开始创建bar_left内容.
    QVBoxLayout *layout_bar_left = new QVBoxLayout;
    bar_left->setLayout(layout_bar_left);

    // 放置程序图标和标题 -----------------------------------
    QHBoxLayout *layout_title = new QHBoxLayout;
    layout_bar_left->addLayout(layout_title);
    layout_title->setContentsMargins(0, 0, 0, 0);

    QLabel *app_icon = new QLabel;
    // 在这里添加载入程序图标的代码
    // To do...
    app_icon->setObjectName(QStringLiteral("app-icon"));
    layout_title->addWidget(app_icon);

    QLabel *app_title = new QLabel;
    app_title->setObjectName(QStringLiteral("app-title"));
    app_title->setText(QObject::tr("app_title"));

    layout_title->addWidget(app_title);
    layout_title->setStretch(0, 1);
    layout_title->setStretch(1, 6);

    // 放置用户信息 ----------------------------------------
    QHBoxLayout *layout_user_info = new QHBoxLayout;
    layout_bar_left->addLayout(layout_user_info);
    layout_user_info->setContentsMargins(10, 0, 10, 0);

    // 头像
    QLabel *head_portrait = new QLabel;
    layout_user_info->addWidget(head_portrait);
    head_portrait->setPixmap(QPixmap(":/img/userinfo/default_portrait"));

    QVBoxLayout *layout_user_info_right = new QVBoxLayout;
    layout_user_info->addLayout(layout_user_info_right);

    QHBoxLayout *layout_username_level = new QHBoxLayout;
    layout_user_info_right->addLayout(layout_username_level);
    layout_username_level->setContentsMargins(10, 10, 10, 10);

    QLabel *username = new QLabel;
    layout_username_level->addWidget(username);
    username->setObjectName("username");
    username->setText("Username");

    QLabel *userlevel = new QLabel;
    layout_username_level->addWidget(userlevel);
    userlevel->setObjectName(QStringLiteral("userlevel"));

    layout_user_info_right->addStretch();

    layout_user_info->setStretch(0, 1);
    layout_user_info->setStretch(1, 3);

    // 开始创建bar_middle内容：界面切换按钮组.
    QHBoxLayout *layout_bar_middle = new QHBoxLayout;
    bar_middle->setLayout(layout_bar_middle);
    layout_bar_middle->setContentsMargins(12, 0, 12, 0);
    layout_bar_middle->setSpacing(0);

    // 互斥按钮组.
    m_bigToolButtons = new QButtonGroup(this);

    // 下面这些按钮宽度是固定的，最好保证最多四个字.
    // 我的网盘
    m_my_netdisk = new BigToolButton(QStringLiteral(":/img/toolButton/my_netdisk"));
    layout_bar_middle->addWidget(m_my_netdisk);
    m_my_netdisk->setText(QObject::tr("My NetDisk"));
    m_my_netdisk->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    m_my_netdisk->setFixedWidth(TOOL_BUTTON_WIDTH);
    // 默认项
    m_my_netdisk->setChecked(true);
    m_bigToolButtons->addButton(m_my_netdisk, PAGE_MY_NETDISK);

    // 分享
    m_share = new BigToolButton(QStringLiteral(":/img/toolButton/share"));
    layout_bar_middle->addWidget(m_share);
    m_share->setText(QObject::tr("Share"));
    m_share->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    m_share->setFixedWidth(TOOL_BUTTON_WIDTH);
    m_bigToolButtons->addButton(m_share, PAGE_SHARE);

    // 隐藏空间
    m_hidden_sapce = new BigToolButton(QStringLiteral(":/img/toolButton/hidden_sapce"));
    layout_bar_middle->addWidget(m_hidden_sapce);
    m_hidden_sapce->setText(QObject::tr("Hidden Space"));
    m_hidden_sapce->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    m_hidden_sapce->setFixedWidth(TOOL_BUTTON_WIDTH);
    m_bigToolButtons->addButton(m_hidden_sapce, PAGE_HIDDEN_SPACE);

    // 功能宝箱
    m_toolbox = new BigToolButton(QStringLiteral(":/img/toolButton/toolbox"));
    layout_bar_middle->addWidget(m_toolbox);
    m_toolbox->setText(QObject::tr("ToolBox"));
    m_toolbox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    m_toolbox->setFixedWidth(TOOL_BUTTON_WIDTH);
    m_bigToolButtons->addButton(m_toolbox, PAGE_TOOLBOX);

    // 弹簧
    layout_bar_middle->addStretch();

    // 开始创建bar_right内容.
    QVBoxLayout *layout_bar_right = new QVBoxLayout;
    bar_right->setLayout(layout_bar_right);
    layout_bar_right->setContentsMargins(0, 0, 0, 0);

    // 系统按钮
    QHBoxLayout *sysbtns = new QHBoxLayout;
    layout_bar_right->addLayout(sysbtns);
    sysbtns->setDirection(QBoxLayout::RightToLeft);
    sysbtns->setSpacing(0);

    m_sysbtn_close = new ImagePushButton;
    m_sysbtn_close->setImageName(QStringLiteral(":img/sysButton/close"));
    m_sysbtn_close->setToolTip(QObject::tr("close"));

    m_sysbtn_max = new ImagePushButton;
    m_sysbtn_max->setImageName(QStringLiteral(":img/sysButton/max"));
    m_sysbtn_max->setToolTip(QObject::tr("maximize"));

    m_sysbtn_restore = new ImagePushButton;
    m_sysbtn_restore->setImageName(QStringLiteral(":img/sysButton/restore"));
    m_sysbtn_restore->setToolTip(QObject::tr("restore"));
    m_sysbtn_restore->hide();

    m_sysbtn_min = new ImagePushButton;
    m_sysbtn_min->setImageName(QStringLiteral(":img/sysButton/min"));
    m_sysbtn_min->setToolTip(QObject::tr("minimize"));

    m_sysbtn_menu = new ImagePushButton;
    m_sysbtn_menu->setImageName(QStringLiteral(":/img/sysButton/menu"));
    m_sysbtn_menu->setObjectName(QObject::tr("menu"));

    sysbtns->addWidget(m_sysbtn_close);
    sysbtns->addWidget(m_sysbtn_max);
    sysbtns->addWidget(m_sysbtn_restore);
    sysbtns->addWidget(m_sysbtn_min);
    sysbtns->addWidget(m_sysbtn_menu);
    sysbtns->addStretch();

    // 常用按钮区
    QVBoxLayout *freq_used_btns = new QVBoxLayout;
    layout_bar_right->addLayout(freq_used_btns);
    freq_used_btns->addStretch();
    QHBoxLayout *layout_download_list = new QHBoxLayout;
    freq_used_btns->addLayout(layout_download_list);
    layout_download_list->setDirection(QBoxLayout::RightToLeft);
    layout_download_list->setContentsMargins(0 ,0 ,0 ,0);
    m_btn_transmit_list = new QToolButton;
    layout_download_list->addWidget(m_btn_transmit_list);
    layout_download_list->addStretch();
    m_btn_transmit_list->setObjectName(QStringLiteral("transmit-list"));
    m_btn_transmit_list->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    m_btn_transmit_list->setText(QObject::tr("Transmit List"));

    // 内容页容器
    // 方案一：按照顺序添加，需要注意顺序。
    // 方案二：使用insertWidget配合index添加，不容易出错。
    m_page_container = new QStackedWidget;
    layout_main->addWidget(m_page_container);

    // 内容页
    // 我的云盘
    QWidget *page_my_netdisk = new QWidget;
    page_my_netdisk->setProperty("class", QStringLiteral("pages"));
    QVBoxLayout *vLay = new QVBoxLayout;
    page_my_netdisk->setLayout(vLay);

    QLabel *label_test = new QLabel(QStringLiteral("My NetDisk"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#9933FF");

    m_page_container->insertWidget(PAGE_MY_NETDISK, page_my_netdisk);

    // 分享
    QWidget *page_share = new QWidget;
    page_share->setProperty("class", QStringLiteral("pages"));
    vLay = new QVBoxLayout;
    page_share->setLayout(vLay);

    label_test = new QLabel(QStringLiteral("Share"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#00CC00;");

    m_page_container->insertWidget(PAGE_SHARE, page_share);

    // 隐藏空间
    QWidget *page_hidden_space = new QWidget;
    page_hidden_space->setProperty("class", QStringLiteral("pages"));
    vLay = new QVBoxLayout;
    page_hidden_space->setLayout(vLay);

    label_test = new QLabel(QStringLiteral("Hidden Space"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#CC3333;");

    m_page_container->insertWidget(PAGE_HIDDEN_SPACE, page_hidden_space);

    // 功能宝箱
    QWidget *page_toolbox = new QWidget;
    page_toolbox->setProperty("class", QStringLiteral("pages"));
    vLay = new QVBoxLayout;
    page_toolbox->setLayout(vLay);

    label_test = new QLabel(QStringLiteral("ToolBox"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#FF6666;");

    m_page_container->insertWidget(PAGE_TOOLBOX, page_toolbox);

    // 传输列表
    QWidget *page_transmit_list = new QWidget;
    page_transmit_list->setProperty("class", QStringLiteral("pages"));
    vLay = new QVBoxLayout;
    page_transmit_list->setLayout(vLay);

    label_test = new QLabel(QStringLiteral("Transmit list"));
    vLay->addWidget(label_test);
    label_test->setAlignment(Qt::AlignCenter);
    label_test->setStyleSheet("font-size:20px; font-weight:500; color:#FF6666;");

    m_page_container->insertWidget(PAGE_TRANSMIT_LIST, page_transmit_list);
}
